// pages/liveList/liveList.js
import {request} from "../../utils/request.js"
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    slideList:[
    
    ],
    page:0,
    liveList:[],
    liveListRight:[],
    liveListLeft:[],
    showLoading: true,
    noMoretip: false,
    showBackBtn:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.calNavBarHeight(that); //计算得出导航栏高度
    var pathArr = getCurrentPages();
    if(pathArr.length<=1){
      that.setData({
        showBackBtn:false
      })
    }
    if (!app.globalData.plumSession) {
      //回调
      app.plumSessionCallback = plumSession => {
        console.log('查询到登录信息');
        that.requestLiveList()
        that.requestliveConfig()
      }
    } else {
      that.requestLiveList()
      that.requestliveConfig()
    }
   
  },
  //返回上一页
  backLastPage(e){
    let type=e.currentTarget.dataset.type;
    if(type=='last'){
      wx.navigateBack({
        delta:1
      })
    }else{
      app.backHome()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  requestliveConfig() {
    request({
      data: {
        map: 'applet_live_cfg',
     
      },
    }).then(res => {
      
     this.setData({
       slideList:res.data.data.slide
     })
      
    }).catch(err => {
      console.log(err);

    })
  },
  requestLiveList(){
    this.setData({
      showLoading:true
    })
   let page=this.data.page
    request({
      data: {
        map: 'applet_live_room_list',
        page:page
      },
      noloading:true,
      noTip:true
    }).then(res => {
      let newList=res.data.data,
      liveListLeft=this.data.liveListLeft,
      liveListRight=this.data.liveListRight
      for(let i=0;i<newList.length;i++){
        if(i%2==0){
          liveListLeft.push(newList[i])
        }else{
          liveListRight.push(newList[i])
        }
      }
      if(page==0){
        var  allList=newList;
      }else{
        var  allList=this.data.liveList.concat(newList)
      }
      
      this.setData({
        liveList:allList,
        showLoading:false,
        liveListLeft:liveListLeft,
        liveListRight:liveListRight
      })
      console.log(this.data.liveList)
      if(newList.length<10){
        this.setData({
          noMoretip:true
        })
      }
      
    }).catch(err => {
      console.log(err);
      this.setData({
        showLoading:false,
        noMoretip:true
      })
    })
  },
  toLiveDetailIn(e){
	  console.log(e);
	let roomId = e.currentTarget.dataset.id;
	let customParams = encodeURIComponent(JSON.stringify({ path: '/subpages0/liverPrediction/liverPrediction'})) ;
	wx.navigateTo({
	url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}&custom_params=${customParams}`
	})  
  },
  //直播详情
  toLiveDetail(e){
    let liveId=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages1/livePlayerDetail/livePlayerDetail?id='+liveId
    })
    this.clearLastLive()
  },
  closeSmallLive(){
    this.setData({
      lastLive:""
    })
    app.globalData.lastLive=""
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(app.globalData.lastLive){
      this.setData({
        lastLive:app.globalData.lastLive
      })
    }
  },
  clearLastLive(){
    this.setData({
      lastLive:""
    })
    app.globalData.lastLive=""
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.clearLastLive()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:0,
      noMoretip:false,
      showLoading:true,
      liveList:[],
      liveListLeft:[],
      liveListRight:[]
    })
    this.requestLiveList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      page++;
      that.setData({
        page: page
      });
      that.requestLiveList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})